<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/lstm_unit_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - lstm_unit_layer.cpp<span style="font-size: 80%;"> (source / <a href="lstm_unit_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">84</td>
            <td class="headerCovTableEntryLo">2.4 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:50:33</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">14</td>
            <td class="headerCovTableEntryLo">14.3 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;algorithm&gt;</a>
<span class="lineNum">       2 </span>            : #include &lt;cmath&gt;
<span class="lineNum">       3 </span>            : #include &lt;vector&gt;
<span class="lineNum">       4 </span>            : 
<span class="lineNum">       5 </span>            : #include &quot;caffe/layer.hpp&quot;
<span class="lineNum">       6 </span>            : #include &quot;caffe/layers/lstm_layer.hpp&quot;
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : namespace caffe {
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      11 </span>            : inline Dtype sigmoid(Dtype x) {
<span class="lineNum">      12 </span><span class="lineNoCov">          0 :   return 1. / (1. + exp(-x));</span>
<span class="lineNum">      13 </span>            : }
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      16 </span>            : inline Dtype tanh(Dtype x) {
<span class="lineNum">      17 </span><span class="lineNoCov">          0 :   return 2. * sigmoid(2. * x) - 1.;</span>
<span class="lineNum">      18 </span>            : }
<a name="19"><span class="lineNum">      19 </span>            : </a>
<span class="lineNum">      20 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      21 </span><span class="lineNoCov">          0 : void LSTMUnitLayer&lt;Dtype&gt;::Reshape(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      22 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      23 </span><span class="lineNoCov">          0 :   const int num_instances = bottom[0]-&gt;shape(1);</span>
<span class="lineNum">      24 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; bottom.size(); ++i) {</span>
<span class="lineNum">      25 </span><span class="lineNoCov">          0 :     if (i == 2) {</span>
<span class="lineNum">      26 </span><span class="lineNoCov">          0 :       CHECK_EQ(2, bottom[i]-&gt;num_axes());</span>
<span class="lineNum">      27 </span>            :     } else {
<span class="lineNum">      28 </span><span class="lineNoCov">          0 :       CHECK_EQ(3, bottom[i]-&gt;num_axes());</span>
<span class="lineNum">      29 </span>            :     }
<span class="lineNum">      30 </span><span class="lineNoCov">          0 :     CHECK_EQ(1, bottom[i]-&gt;shape(0));</span>
<span class="lineNum">      31 </span><span class="lineNoCov">          0 :     CHECK_EQ(num_instances, bottom[i]-&gt;shape(1));</span>
<span class="lineNum">      32 </span>            :   }
<span class="lineNum">      33 </span><span class="lineNoCov">          0 :   hidden_dim_ = bottom[0]-&gt;shape(2);</span>
<span class="lineNum">      34 </span><span class="lineNoCov">          0 :   CHECK_EQ(4 * hidden_dim_, bottom[1]-&gt;shape(2));</span>
<span class="lineNum">      35 </span><span class="lineNoCov">          0 :   top[0]-&gt;ReshapeLike(*bottom[0]);</span>
<span class="lineNum">      36 </span><span class="lineNoCov">          0 :   top[1]-&gt;ReshapeLike(*bottom[0]);</span>
<span class="lineNum">      37 </span><span class="lineNoCov">          0 :   X_acts_.ReshapeLike(*bottom[1]);</span>
<span class="lineNum">      38 </span><span class="lineNoCov">          0 : }</span>
<a name="39"><span class="lineNum">      39 </span>            : </a>
<span class="lineNum">      40 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      41 </span><span class="lineNoCov">          0 : void LSTMUnitLayer&lt;Dtype&gt;::Forward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      42 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      43 </span><span class="lineNoCov">          0 :   const int num = bottom[0]-&gt;shape(1);</span>
<span class="lineNum">      44 </span><span class="lineNoCov">          0 :   const int x_dim = hidden_dim_ * 4;</span>
<span class="lineNum">      45 </span><span class="lineNoCov">          0 :   const Dtype* C_prev = bottom[0]-&gt;cpu_data();</span>
<span class="lineNum">      46 </span><span class="lineNoCov">          0 :   const Dtype* X = bottom[1]-&gt;cpu_data();</span>
<span class="lineNum">      47 </span><span class="lineNoCov">          0 :   const Dtype* cont = bottom[2]-&gt;cpu_data();</span>
<span class="lineNum">      48 </span><span class="lineNoCov">          0 :   Dtype* C = top[0]-&gt;mutable_cpu_data();</span>
<span class="lineNum">      49 </span><span class="lineNoCov">          0 :   Dtype* H = top[1]-&gt;mutable_cpu_data();</span>
<span class="lineNum">      50 </span><span class="lineNoCov">          0 :   for (int n = 0; n &lt; num; ++n) {</span>
<span class="lineNum">      51 </span><span class="lineNoCov">          0 :     for (int d = 0; d &lt; hidden_dim_; ++d) {</span>
<span class="lineNum">      52 </span><span class="lineNoCov">          0 :       const Dtype i = sigmoid(X[d]);</span>
<span class="lineNum">      53 </span>            :       const Dtype f = (*cont == 0) ? 0 :
<span class="lineNum">      54 </span><span class="lineNoCov">          0 :           (*cont * sigmoid(X[1 * hidden_dim_ + d]));</span>
<span class="lineNum">      55 </span><span class="lineNoCov">          0 :       const Dtype o = sigmoid(X[2 * hidden_dim_ + d]);</span>
<span class="lineNum">      56 </span><span class="lineNoCov">          0 :       const Dtype g = tanh(X[3 * hidden_dim_ + d]);</span>
<span class="lineNum">      57 </span><span class="lineNoCov">          0 :       const Dtype c_prev = C_prev[d];</span>
<span class="lineNum">      58 </span><span class="lineNoCov">          0 :       const Dtype c = f * c_prev + i * g;</span>
<span class="lineNum">      59 </span><span class="lineNoCov">          0 :       C[d] = c;</span>
<span class="lineNum">      60 </span>            :       const Dtype tanh_c = tanh(c);
<span class="lineNum">      61 </span><span class="lineNoCov">          0 :       H[d] = o * tanh_c;</span>
<span class="lineNum">      62 </span>            :     }
<span class="lineNum">      63 </span><span class="lineNoCov">          0 :     C_prev += hidden_dim_;</span>
<span class="lineNum">      64 </span><span class="lineNoCov">          0 :     X += x_dim;</span>
<span class="lineNum">      65 </span><span class="lineNoCov">          0 :     C += hidden_dim_;</span>
<span class="lineNum">      66 </span><span class="lineNoCov">          0 :     H += hidden_dim_;</span>
<span class="lineNum">      67 </span><span class="lineNoCov">          0 :     ++cont;</span>
<span class="lineNum">      68 </span>            :   }
<span class="lineNum">      69 </span><span class="lineNoCov">          0 : }</span>
<a name="70"><span class="lineNum">      70 </span>            : </a>
<span class="lineNum">      71 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      72 </span><span class="lineNoCov">          0 : void LSTMUnitLayer&lt;Dtype&gt;::Backward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,</span>
<span class="lineNum">      73 </span>            :     const vector&lt;bool&gt;&amp; propagate_down, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) {
<span class="lineNum">      74 </span><span class="lineNoCov">          0 :   CHECK(!propagate_down[2]) &lt;&lt; &quot;Cannot backpropagate to sequence indicators.&quot;;</span>
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :   if (!propagate_down[0] &amp;&amp; !propagate_down[1]) { return; }</span>
<span class="lineNum">      76 </span>            : 
<span class="lineNum">      77 </span><span class="lineNoCov">          0 :   const int num = bottom[0]-&gt;shape(1);</span>
<span class="lineNum">      78 </span><span class="lineNoCov">          0 :   const int x_dim = hidden_dim_ * 4;</span>
<span class="lineNum">      79 </span><span class="lineNoCov">          0 :   const Dtype* C_prev = bottom[0]-&gt;cpu_data();</span>
<span class="lineNum">      80 </span><span class="lineNoCov">          0 :   const Dtype* X = bottom[1]-&gt;cpu_data();</span>
<span class="lineNum">      81 </span><span class="lineNoCov">          0 :   const Dtype* cont = bottom[2]-&gt;cpu_data();</span>
<span class="lineNum">      82 </span><span class="lineNoCov">          0 :   const Dtype* C = top[0]-&gt;cpu_data();</span>
<span class="lineNum">      83 </span><span class="lineNoCov">          0 :   const Dtype* H = top[1]-&gt;cpu_data();</span>
<span class="lineNum">      84 </span><span class="lineNoCov">          0 :   const Dtype* C_diff = top[0]-&gt;cpu_diff();</span>
<span class="lineNum">      85 </span><span class="lineNoCov">          0 :   const Dtype* H_diff = top[1]-&gt;cpu_diff();</span>
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :   Dtype* C_prev_diff = bottom[0]-&gt;mutable_cpu_diff();</span>
<span class="lineNum">      87 </span><span class="lineNoCov">          0 :   Dtype* X_diff = bottom[1]-&gt;mutable_cpu_diff();</span>
<span class="lineNum">      88 </span><span class="lineNoCov">          0 :   for (int n = 0; n &lt; num; ++n) {</span>
<span class="lineNum">      89 </span><span class="lineNoCov">          0 :     for (int d = 0; d &lt; hidden_dim_; ++d) {</span>
<span class="lineNum">      90 </span><span class="lineNoCov">          0 :       const Dtype i = sigmoid(X[d]);</span>
<span class="lineNum">      91 </span>            :       const Dtype f = (*cont == 0) ? 0 :
<span class="lineNum">      92 </span><span class="lineNoCov">          0 :           (*cont * sigmoid(X[1 * hidden_dim_ + d]));</span>
<span class="lineNum">      93 </span><span class="lineNoCov">          0 :       const Dtype o = sigmoid(X[2 * hidden_dim_ + d]);</span>
<span class="lineNum">      94 </span><span class="lineNoCov">          0 :       const Dtype g = tanh(X[3 * hidden_dim_ + d]);</span>
<span class="lineNum">      95 </span><span class="lineNoCov">          0 :       const Dtype c_prev = C_prev[d];</span>
<span class="lineNum">      96 </span><span class="lineNoCov">          0 :       const Dtype c = C[d];</span>
<span class="lineNum">      97 </span>            :       const Dtype tanh_c = tanh(c);
<span class="lineNum">      98 </span><span class="lineNoCov">          0 :       Dtype* c_prev_diff = C_prev_diff + d;</span>
<span class="lineNum">      99 </span><span class="lineNoCov">          0 :       Dtype* i_diff = X_diff + d;</span>
<span class="lineNum">     100 </span><span class="lineNoCov">          0 :       Dtype* f_diff = X_diff + 1 * hidden_dim_ + d;</span>
<span class="lineNum">     101 </span><span class="lineNoCov">          0 :       Dtype* o_diff = X_diff + 2 * hidden_dim_ + d;</span>
<span class="lineNum">     102 </span><span class="lineNoCov">          0 :       Dtype* g_diff = X_diff + 3 * hidden_dim_ + d;</span>
<span class="lineNum">     103 </span>            :       const Dtype c_term_diff =
<span class="lineNum">     104 </span><span class="lineNoCov">          0 :           C_diff[d] + H_diff[d] * o * (1 - tanh_c * tanh_c);</span>
<span class="lineNum">     105 </span><span class="lineNoCov">          0 :       *c_prev_diff = c_term_diff * f;</span>
<span class="lineNum">     106 </span><span class="lineNoCov">          0 :       *i_diff = c_term_diff * g * i * (1 - i);</span>
<span class="lineNum">     107 </span><span class="lineNoCov">          0 :       *f_diff = c_term_diff * c_prev * f * (1 - f);</span>
<span class="lineNum">     108 </span><span class="lineNoCov">          0 :       *o_diff = H_diff[d] * tanh_c * o * (1 - o);</span>
<span class="lineNum">     109 </span><span class="lineNoCov">          0 :       *g_diff = c_term_diff * i * (1 - g * g);</span>
<span class="lineNum">     110 </span>            :     }
<span class="lineNum">     111 </span><span class="lineNoCov">          0 :     C_prev += hidden_dim_;</span>
<span class="lineNum">     112 </span><span class="lineNoCov">          0 :     X += x_dim;</span>
<span class="lineNum">     113 </span><span class="lineNoCov">          0 :     C += hidden_dim_;</span>
<span class="lineNum">     114 </span>            :     H += hidden_dim_;
<span class="lineNum">     115 </span><span class="lineNoCov">          0 :     C_diff += hidden_dim_;</span>
<span class="lineNum">     116 </span><span class="lineNoCov">          0 :     H_diff += hidden_dim_;</span>
<span class="lineNum">     117 </span><span class="lineNoCov">          0 :     X_diff += x_dim;</span>
<span class="lineNum">     118 </span><span class="lineNoCov">          0 :     C_prev_diff += hidden_dim_;</span>
<span class="lineNum">     119 </span><span class="lineNoCov">          0 :     ++cont;</span>
<span class="lineNum">     120 </span>            :   }
<span class="lineNum">     121 </span>            : }
<a name="122"><span class="lineNum">     122 </span>            : </a>
<span class="lineNum">     123 </span>            : #ifdef CPU_ONLY
<span class="lineNum">     124 </span><span class="lineNoCov">          0 : STUB_GPU(LSTMUnitLayer);</span>
<span class="lineNum">     125 </span>            : #endif
<a name="126"><span class="lineNum">     126 </span>            : </a>
<span class="lineNum">     127 </span>            : INSTANTIATE_CLASS(LSTMUnitLayer);
<a name="128"><span class="lineNum">     128 </span><span class="lineCov">          3 : REGISTER_LAYER_CLASS(LSTMUnit);</span></a>
<span class="lineNum">     129 </span>            : 
<span class="lineNum">     130 </span><span class="lineCov">          3 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
